<?php
/**
 * Class that handles request input filtering
 * @author Wavetrex (wavetrex@gmail.com)
 * @package CargoClaims
 * @version 1.0.dev
 */
class MVC_Filter
{
	/**
	 * What kind of filter this is ?
	 *
	 * @var string
	 */
	private $type;

	/**
	 * Optional filter configuration
	 * @var mixed
	 */
	private $cfg;

	public function __construct($filter_type, $filter_cfg = null)
	{
		if(method_exists($this, 'filter_'.$filter_type)) {
			$this-> type = $filter_type;
			$this-> cfg = $filter_cfg;
		}
	}

	/**
	 * Processes input according to the type of filter
	 *
	 * @param mixed $input_data
	 */
	public function process($input_data)
	{
		if(!empty($this-> type)) {

			// process input recursively if it's an array (apply the filters on all members)
			if(is_array($input_data)) {
				foreach($input_data as &$input_member)
					$input_member = $this-> process($input_member);
			} else
				return call_user_func(array($this, 'filter_'.$this-> type), $input_data);
		} else {
			// returns the data as is, but @todo Shoot a notification that an unexisting filter type was attempted to be used
			return $input_data;
		}
	}

	// Filtering methods

	/**
	 * Checks if the Input is a number, and returns null if not
	 * @param mixed $input
	 */
	private function filter_Is_Numeric($input)
	{
		return is_numeric($input)?$input:null;
	}

	/**
	 * Checks if the input is a valid Claim tracking ID, in the format "AAA-AAA-AAAA", where A might be [A-Z0-9]
	 * @param mixed $input
	 */
	private function filter_Is_TrackID($input)
	{
		return preg_match("/^[A-Z0-9]{3}-[A-Z0-9]{3}-[A-Z0-9]{4}$/", $input)?$input:null;
	}
}